package servlet;

import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/SearchAllServlet.do")
public class SearchAllServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Map<String, Object>> searchResults = performQuery("SELECT * FROM user", null);

        HttpSession session = request.getSession();
        session.setAttribute("searchResults", searchResults);

        response.sendRedirect(request.getContextPath() + "/background1/admin/usermanager.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    private List<Map<String, Object>> performQuery(String sql, String parameter) {

        List<Map<String, Object>> userList = new ArrayList<>();
        JDBCUtil jdbcUtil = new JDBCUtil();
        ResultSet resultSet = null;

        try {
            resultSet = parameter == null ? jdbcUtil.prepareQuery(sql) : jdbcUtil.prepareQuery(sql, parameter);

            while (resultSet != null && resultSet.next()) {
                int id = resultSet.getInt("user_id");
                String username = resultSet.getString("username");
                String email = resultSet.getString("email");
                String phone = resultSet.getString("phone");
                String created_at = resultSet.getString("created_at");
                int user_type = resultSet.getInt("user_type");

                Map<String, Object> user = Map.of(
                        "id", id,
                        "username", username,
                        "email", email,
                        "phone", phone,
                        "created_at", created_at,
                        "user_type", user_type
                );
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            try {
                if (resultSet != null) resultSet.close();
                jdbcUtil.closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return userList;
    }

    private List<Map<String, Object>> performSearch(String searchUsername) {
        String sql = "SELECT * FROM user WHERE username = ?";
        return performQuery(sql, searchUsername);
    }

    private List<Map<String, Object>> performSearchid(String id) {
        String sql = "SELECT * FROM user WHERE user_id = ?";
        return performQuery(sql, id);
    }
}
